/**
 * @author Anubhab(Infosys)
 * Created: Jan 3, 2013
 */
package com.pojocreator;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pojocreator.common.AccessProperties;
import com.pojocreator.dao.ConnectionManager;
import com.pojocreator.dao.TableExplorer;
import com.pojocreator.dto.TableDTO;
import com.pojocreator.processor.JavaCreatorImpl;
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JAssignmentTarget;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;

/**
 * @author Anubhab(Infosys)
 *
 */
public class MainClass {
	
	private static final Log log = LogFactory.getLog(MainClass.class);
	/**
	 * @param args
	 * 
	 * @author Anubhab(Infosys)
	 * Created: Jan 3, 2013
	 */
	public static void main(String[] args) {
		log.info("Checking the Log Creation");
		String username=AccessProperties.getValueFromProps("db.Username");
		String password=AccessProperties.getValueFromProps("db.Password");
		String connUrl=AccessProperties.getValueFromProps("db.URL");
		
		ConnectionManager conMgr=new ConnectionManager(connUrl, username, password);
		if(null==conMgr.getConnection()){
			log.info("Connection Establishment in Error.");
		} else {
			TableExplorer tablExplr=new TableExplorer(conMgr);
			List<TableDTO> tableList=tablExplr.getAllTableDescription();
			try {
				conMgr.closeConnection();
			} catch (SQLException e) {
				log.error("",e);
			}
			
			JavaCreatorImpl creator=new JavaCreatorImpl();
			for(TableDTO temp:tableList){
				creator.createClassFile(temp,"ALL_CLASS");
			}
		}
		
		/*for(TableDTO temp:tableMap){
			log.info(temp);
		}*/
		/*Iterator it = tableMap.entrySet().iterator();
		
		JCodeModel cm = null;
		try {
			cm = new JCodeModel();
			JDefinedClass dc = cm._class(JMod.PUBLIC,"foo."+tableName,ClassType.CLASS);
			
			//log.info("Name         Type");
			//log.info("-------------------");
			while(it.hasNext()){
				Map.Entry pairs = (Map.Entry)it.next();
				//log.info(pairs.getKey()+"-----"+pairs.getValue());
				//TODO: Map sql to java data type
				String varName=pairs.getKey().toString();
				String dataType=pairs.getValue().toString();
				varName=varName.toLowerCase();
				Class<?> type=String.class;
				//Create field
				if(dataType.equalsIgnoreCase("VARCHAR2")){
					type=String.class;
				} else if(dataType.equalsIgnoreCase("NUMBER")){
					type=int.class;
				} else if(dataType.equalsIgnoreCase("Date")){
					type=Date.class;
				}else if(dataType.contains("TIMESTAMP")){
					type=Timestamp.class;
				}
				
				dc.field(JMod.PRIVATE,type,varName);
				
				//Create getter
				JMethod m = dc.method(JMod.PUBLIC, type, "get"+varName);
				m.body()._return(JExpr.refthis(varName));
				
				//Create Setter
				JMethod m1 = dc.method(JMod.PUBLIC,void.class,"set"+varName);
				m1.param(String.class, varName);
				m1.body().assign(JExpr.refthis(varName),JExpr.ref(varName));
			}
		
		
		
			//dc.field(JMod.PRIVATE, String.class,"name");
			//dc.field(JMod.PRIVATE, int.class,"age");

			Map<String,JFieldVar> fields=dc.fields();
			
			List<JFieldVar> ss=(List<JFieldVar>) fields.values();
			for(JFieldVar var:ss){
				String name=var.name();
				log.info("Name:"+name);
			}
		
			JMethod m = dc.method(JMod.PUBLIC, String.class, "getName");
			m.body()._return(JExpr.refthis("name"));
			
			JMethod m1 = dc.method(JMod.PUBLIC,void.class,"setName");
			m1.param(String.class, "name");
			m1.body().assign(JExpr.refthis("name"),JExpr.ref("name"));
			
		} catch (JClassAlreadyExistsException e) {
			log.error("",e);
		}

		File file = new File("./classes");
		file.mkdirs();
		try {
			cm.build(file);
		} catch (IOException e) {
			log.error("",e);
		}*/
	}

}
